package com.smarttest.portal.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.g7s.test.utils.common.CommonUtils;
import com.smarttest.common.util.DateTimeUtils;
import com.smarttest.portal.mapper.DbAutoBuildplatformMapper;
import com.smarttest.portal.mapper.DbAutoManualresultMapper;
import com.smarttest.portal.pojo.DbAutoBuildplatform;
import com.smarttest.portal.pojo.DbAutoBuildplatformExample;
import com.smarttest.portal.pojo.DbAutoBuildplatformExample.Criteria;
import com.smarttest.portal.pojo.DbAutoManualresult;
import com.smarttest.portal.pojo.DbAutoManualresultExample;
import com.smarttest.portal.service.TestAutoBuildService;
@Service
public class TestAutoBuildServiceImpl implements TestAutoBuildService {

	@Autowired
	DbAutoBuildplatformMapper dbAutoBuildplatformMapper;
	@Autowired
	DbAutoManualresultMapper dbAutoManualresultMapper;
	@Override
	public String getAllGroups() {
		// TODO Auto-generated method stub
		String allgroupsHtml = "<option value=\"ALL\" selected=\"selected\">-ALL-</option>";
		DbAutoBuildplatformExample example = new DbAutoBuildplatformExample();
		example.setDistinct(true);
		List<String> l = dbAutoBuildplatformMapper.selectGroupsByExample(example);
		for (String d : l) {
			allgroupsHtml += "<option value=\""+d+"\">"+d+"</option>";
		}
		return allgroupsHtml;
	}

	@Override
	public String getService(String group) {
		// TODO Auto-generated method stub
		String serviceHtml = "";
		DbAutoBuildplatformExample example = new DbAutoBuildplatformExample();
		example.setDistinct(true);
		if (group != null) {
			Criteria c = example.createCriteria();
			c.andDgroupEqualTo(group);
		}
		List<String> l = dbAutoBuildplatformMapper.selectServiceByExample(example);
		for (String d : l) {
			serviceHtml += "<option value=\""+d+"\">"+d+"</option>";
		}
		return serviceHtml;
	}

	@Override
	public List<DbAutoBuildplatform> getAllBuilds(String group, String service, String testenv, String time,
			String issuedstatus) {
		// TODO Auto-generated method stub
		List<String> servicelist = JSON.parseArray(service, String.class);
		List<Integer> testenvlist = JSON.parseArray(testenv, Integer.class);
		List<Integer> issuedstatuslist = JSON.parseArray(issuedstatus, Integer.class);
		String []timearr = time.replaceAll("\"", "").split(" - ");
		DbAutoBuildplatformExample example = new DbAutoBuildplatformExample();
		example.setOrderByClause("buildtime desc");
		Criteria c = example.createCriteria();
		if (!group.isEmpty()) {
			c.andDgroupEqualTo(group);
		}
		if (null != servicelist) {
			c.andDserviceIn(servicelist);
		}
		if (null != testenvlist) {
			c.andTestenvIn(testenvlist);
		}
		if (null != issuedstatuslist) {
			c.andIssuedpermissionIn(issuedstatuslist);
		}
		c.andBuildtimeGreaterThanOrEqualTo(DateTimeUtils.text2Date(timearr[0], "yy-MM-dd HH:mm"));
		c.andBuildtimeLessThanOrEqualTo(DateTimeUtils.text2Date(timearr[1],"yy-MM-dd HH:mm"));
		return dbAutoBuildplatformMapper.selectByExample(example);
	}

	@Override
	public int updateManualReport(int buildplatformId, int testresult, String casefile, String bugUrl) {
		// TODO Auto-generated method stub
		int tmpcount = 0;
		DbAutoBuildplatform db = dbAutoBuildplatformMapper.selectByPrimaryKey(buildplatformId);
		int manualresultid =  db.getManualresultid();
		if (manualresultid > 0) {
			db.setManualresult(testresult);
			int tmpcount2 = dbAutoBuildplatformMapper.updateByPrimaryKeySelective(db);
			if (tmpcount2 == -1) {
				return -1;
			}
			DbAutoManualresult dresult = dbAutoManualresultMapper.selectByPrimaryKey(manualresultid);
			dresult.setResult(testresult);
			dresult.setCasefile(casefile);
			dresult.setBugadd(bugUrl);
			tmpcount = dbAutoManualresultMapper.updateByPrimaryKeySelective(dresult);
		}
		else {
			DbAutoManualresult dresult = new DbAutoManualresult();
			dresult.setResult(testresult);
			dresult.setCasefile(casefile);
			dresult.setBugadd(bugUrl);
			int tmpint = dbAutoManualresultMapper.insertSelective(dresult);
			if (tmpint == 1) {
				db.setManualresultid(dresult.getId());
				db.setManualresult(testresult);
				tmpcount = dbAutoBuildplatformMapper.updateByPrimaryKeySelective(db);
			}
			else if (tmpint == -1){
				tmpcount = -1;
			}
		}
		return tmpcount;
	}

	@Override
	public DbAutoManualresult getManualReport(int buildplatformId) {
		// TODO Auto-generated method stub
		DbAutoManualresult dbAutoManualresult = null;
		int manualresultid = dbAutoBuildplatformMapper.selectByPrimaryKey(buildplatformId).getManualresultid();
		if (manualresultid > 0) {
			dbAutoManualresult = dbAutoManualresultMapper.selectByPrimaryKey(manualresultid);
		}
		return dbAutoManualresult;
	}

	@Override
	public int rejectIssue(int buildplatformId) {
		// TODO Auto-generated method stub
		DbAutoBuildplatform dbp = dbAutoBuildplatformMapper.selectByPrimaryKey(buildplatformId);
		dbp.setIssuedpermission(2);
		return dbAutoBuildplatformMapper.updateByPrimaryKeySelective(dbp);
	}

	@Override
	public int agreeIssue(int buildplatformId) {
		// TODO Auto-generated method stub
		DbAutoBuildplatform dbp = dbAutoBuildplatformMapper.selectByPrimaryKey(buildplatformId);
		dbp.setIssuedpermission(1);
		return dbAutoBuildplatformMapper.updateByPrimaryKeySelective(dbp);
	}

}
